<template>
  <el-dialog title="新增员工" :visible="showDialog" @close="btnCanel">
    <!-- 表单 -->
    <el-form ref="formData" label-width="120px" :model="formData" :rules="rules">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="formData.username" style="width:88%" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="formData.mobile" style="width:88%" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker v-model="formData.timeOfEntry" style="width:88%" placeholder="请选择入职时间" />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="formData.formOfEmployment" style="width:88%" placeholder="请选择">
          <el-option v-for="item in EmploryeesNum.hireType" :key="item.id" :label="item.value" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="formData.workNumber" style="width:88%" placeholder="请输入工号" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input v-model="formData.departmentName" style="width:88%" placeholder="请选择部门" @focus="getDepartment" />
        <el-tree v-if="showTree" class="deptsList" :data="depts" :props="{label:'name'}" :default-expand-all="true" @node-click="selectDepartment" />
      </el-form-item>
      <el-form-item label="转正时间">
        <el-date-picker v-model="formData.correctionTime" style="width:88%" placeholder="请选择转正时间" />
      </el-form-item>
    </el-form>
    <!-- footer插槽 -->
    <template v-slot:footer>
      <el-row type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" @click="btnCanel">取消</el-button>
          <el-button type="primary" size="small" @click="btnOk">确定</el-button>
        </el-col>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
// 处理部门数据
// 1. 请求api
import { getDepartments } from '@/api/departments'
// 2. 数据处理成树形结构
import { listToTreeData } from '@/utils'
// 引入枚举处理聘用形式
import EmploryeesNum from '@/api/constant/employees'
// 添加员工api
import { addEmployee } from '@/api/employees'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      formData: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      },
      rules: {
        username: [{ required: true, message: '用户姓名不能为空', trigger: 'blur' }, {
          min: 1, max: 4, message: '用户姓名为1-4位'
        }],
        mobile: [{ required: true, message: '手机号不能为空', trigger: 'blur' }, {
          pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur'
        }],
        formOfEmployment: [{ required: true, message: '聘用形式不能为空', trigger: 'blur' }],
        workNumber: [{ required: true, message: '工号不能为空', trigger: 'blur' }],
        departmentName: [{ required: true, message: '部门不能为空', trigger: 'change' }],
        timeOfEntry: [{ required: true, message: '入职时间', trigger: 'blur' }]
      },
      depts: [], // 树形结构的部门数据-
      showTree: false, // 是否显示树形组件
      EmploryeesNum // 引入的枚举数据必须先定义才能在模板中使用
    }
  },
  methods: {
    // 获取部门数据
    async getDepartment() {
      // 发请求获取数据
      const { depts } = await getDepartments()
      // 使用封装的方法，把扁平的数据处理成树形结构的
      this.depts = listToTreeData(depts, '')
      // console.log(this.depts)
      // 让树形组件显示
      this.showTree = true
    },
    // 点击树形组件中的节点触发的函数
    selectDepartment(data) {
      // 给部门赋值
      this.formData.departmentName = data.name
      // 关闭树形组件
      this.showTree = false
    },
    // 点击确认
    async btnOk() {
      // 校验表单
      this.$refs.formData.validate()
      // 发请求
      await addEmployee(this.formData)
      // 设定最新的total
      // 如果原来的 total 是10 分页组件认为有 5 页
      // ++ 会改变元数据 之后分页组件就知道有6页了，再去计算和翻页，就没问题了
      this.$parent.pageSetting.total++
      this.$parent.pageSetting.page = Math.ceil(this.$parent.pageSetting.total / this.$parent.pageSetting.size)

      // 提示成功
      this.$message.success('添加成功')
      // 当前组件是子组件
      // 负责渲染的是父组件,正常情况下需要 $emit 通知父组件处理
      // 另外 this.$parent 可以拿到父组件的属性和方法，直接调用
      // 但是不建议常用，因为局限性太大，与父组件绑定，一错全错
      // 通知父组件关闭弹框
      this.$parent.showDialog = false
      // 通知父组件更新数据
      this.$parent.getEmployeeList()
    },
    // 点击关闭，取消
    btnCanel() {
      // 清空表单数据
      this.formData = {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      }
      // 清空表单校验
      this.$refs.formData.resetFields()
      // 关闭弹窗
      this.$emit('update:showDialog', false)
    }
  }
}
</script>

<style lang="scss" scoped>
.deptsList{
  position: absolute;
    width: 88%;
    z-index: 9;
    height: 150px;
    overflow-y: scroll;
    border: 1px solid #ccc;
}
</style>
